const getDefaultState = () => {
	return {
		comments: []
	}
}

const state = getDefaultState();

const mutations = {
	SET_COMMENTS(state, comments) {
		state.comments = comments
	},
	RESET_STATE(state) {
		Object.assign(state, getDefaultState())
	}
}

const actions = {
	async addComment({ commit }, data) {
		let result = await uni.$u.http.post('/comment/add', { ...data, creationTime: Date.now() });
		let { code } = result.data
		return code;
	},
	async addCommentReply({ commit }, data) {
		let result = await uni.$u.http.post('/comment/reply/add', { ...data, creationTime: Date.now() });
		let { code } = result.data
		return code;
	},
	async getComment({ commit }, data) {
		let result = await uni.$u.http.get('/comment/get', { params: { ...data } });
		let { code } = result.data;
		if (code === 200) {
			commit('SET_COMMENTS', result.data.comments);
		}
		return code;
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
